package com.xiaoxiong.gobang.control;

import android.util.Log;

import com.xiaoxiong.gobang.bean.GameState;
import com.xiaoxiong.gobang.bean.Player;
import com.xiaoxiong.gobang.ui.GameViewInterface;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxiong
 * @Package com.xiaoxiong.gobang.control
 * @Description:下棋控制器
 * @email 249900679@qq.com
 * @date 2016/4/2 14:14
 */
public class PutChessControl implements ChessPosition {

    private int[] m_board;
    private List<Integer> QiZis;
    private GameState gameState;
    private GameViewInterface gameViewInterface;

    public PutChessControl() {
        this.QiZis = new ArrayList<Integer>();
        this.m_board = new int[15*15+34+15];
        for(int i = 0;i < 15;++i){
            for(int j = 0;j < 15;++j){
                m_board[17+i+j*16] = -1;
            }
        }
    }

    public void setGameViewInterface(GameViewInterface gameViewInterface) {
        this.gameViewInterface = gameViewInterface;
    }

    public void setGameState(GameState gameState) {
        this.gameState = gameState;
    }

    /**
     * 悔棋
     */
    public void BackMove(){
        if(!QiZis.isEmpty()){
            int pos = QiZis.get(QiZis.size() - 1);
            QiZis.remove(QiZis.size() - 1);
            this.m_board[pos] = Player.NULLPALYER;
            this.gameState.SwitchPlayer();
            this.gameState.SetBoard(m_board);
        }
    }

//    @Override
//    public void setBoard(int[] board, int pos) {
//        QiZis.add(pos);
//        m_board = board;
//        QiZis.add(pos);
//        m_board = board;
//
//        String str = "";
//        for(int i = 0;i < board.length;++i){
//            if(board[i] != 0){
//                str += (i + "(" + board[i] + ")--");
//            }
//        }
//        Log.d("putchess","棋盘：" + str);
//    }

    @Override
    public void setPos(int pos, int playerId) {
        QiZis.add(pos);
        for(int i = 17;i < 256;++i){
            if(m_board[i] > 5){
                m_board[i] -= 5;
            }
        }
        m_board[pos] = playerId + 5;
        gameViewInterface.putChess(m_board);
    }

    @Override
    public void SetWinner(Player player) {
        if(player == null) {
            this.gameViewInterface.GameOver(null);
        }else{
            String winnerName = player.GetPlayerName();
            this.gameViewInterface.GameOver(winnerName);
        }
    }

    @Override
    public void SetName(String name) {
        Log.d("pname",name);
        this.gameViewInterface.setText(name);
    }
}
